package com.xz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xz.entity.Comment;
import com.xz.entity.Video;
import com.xz.entity.frontvo.CommentFrontVo;
import com.xz.mapper.CommentMapper;
import com.xz.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-11
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private VideoService videoService;
    @Override
    public Map<String,Object> getAllComment(String videoId, long current) {
        Map<String,Object> map=new HashMap<>();
        QueryWrapper<Comment> wrapper=new QueryWrapper<>();
        Page<Comment> page = new Page<>(current,5);
        wrapper.eq("video_id",videoId);
        wrapper.orderByDesc("gmt_create");
        IPage<Comment> pageInfo = this.baseMapper.selectPage(page,wrapper);
        List<CommentFrontVo> commentFrontVoList=new ArrayList<>();
        List<Comment> records = pageInfo.getRecords();
        long total = pageInfo.getTotal();
        Video video = videoService.getById(videoId);
        video.setViewCount(total);
        videoService.updateById(video);
        for (Comment comment:records){
            CommentFrontVo commentFrontVo=new CommentFrontVo();
            BeanUtils.copyProperties(comment,commentFrontVo);
            commentFrontVoList.add(commentFrontVo);
        }
        map.put("total",total);
        map.put("records",commentFrontVoList);
        return map;
    }
}
